package owg.util.opengl.models;

import java.nio.FloatBuffer;

/**
 * This class is a container for a vertex using 32 bit floats for data. 
 * */
public class VertexF implements VertexFloat
{
	public float[] vertex;
	public float[] normal;
	public float[] color;
	public float[] texCoord;
	public float[] texCoord2;
	
	/**Construct a vertex with the given data.
	 * If certain components are known to be unnecessary for the application, those may be null.*/
	public VertexF(float[] v,float[] n,float[] c,float[] t, float[] u)
	{
		vertex  = v;
		normal  = n;
		color   = c;
		texCoord= t;
		texCoord2 = u;
	}
	@Override
	public VertexF clone()
	{
		return new VertexF(vertex==null?null:vertex.clone(),normal==null?null:normal.clone(),
			color==null?null:color.clone(),texCoord==null?null:texCoord.clone(),texCoord2==null?null:texCoord2.clone());
	}
	/**Clone the vertex, but don't clone the backing arrays*/
	public VertexF lazyClone()
	{
		return new VertexF(vertex,normal,color,texCoord,texCoord2);
	}
	@Override
	public void put(FloatBuffer vBuf, int numVertexComponents,
		boolean useNormal, byte numColorComponents, byte numTexCoordComponents,
		byte numTexCoordComponents2)
	{
		vBuf.put(vertex, 0, numVertexComponents);
		if (useNormal)
			vBuf.put(normal, 0, 3);
		if (numColorComponents > 0)
		{//Accomodate for missing color compontents
			if(color.length < numColorComponents)
			{
				vBuf.put(color, 0, color.length);
				float[] tmp = new float[numColorComponents - color.length];
				tmp[tmp.length-1] = 1.0f;
				vBuf.put(tmp);
			}
			else
				vBuf.put(color, 0, numColorComponents);
		}
		if (numTexCoordComponents > 0)
			vBuf.put(texCoord, 0, numTexCoordComponents);
		if (numTexCoordComponents2 > 0)
			vBuf.put(texCoord2, 0, numTexCoordComponents2);
	}
}
